package com.hustbaidu.hustmap;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.hustbaidu.hustmap.HustMapView.OnPlaceLabelClickListener;

import android.R.layout;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

public class HustMapActivity extends Activity {
	private HustMapView mapView;
	private MySurfaceView m;
	private HustMapView hustMapView;
	
	private ImageView placeImgWidget;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
//        mapView = new HustMapView(HustMapActivity.this);
//        setContentView(mapView);
//        m = new MySurfaceView(HustMapActivity.this);
//        setContentView(m);
        hustMapView = new HustMapView(HustMapActivity.this);
        setContentView(hustMapView);
        
        hustMapView.setOnPlaceLabelClickListener(new OnPlaceLabelClickListener() {
			
			@Override
			public void onClick(PlaceInfo place, PlaceType type) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
//				bundle.putInt("place_id", place.getId());
				bundle.putParcelable("place", place);
				bundle.putParcelable("type", type);
//				if(type == null){
//					System.out.println("type null");
//				}
				intent.putExtras(bundle);
				intent.setClass(HustMapActivity.this, PlaceInfoActivity.class);
				startActivityForResult(intent, 0);
			}
		});
        
        LectureManager.init(HustMapActivity.this);
        CommentManager.init(HustMapActivity.this);
        ScoreManager.init(HustMapActivity.this);
    }
    
    
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
//		super.onActivityResult(requestCode, resultCode, data);
    	switch (requestCode) {
		case 0:
			switch (resultCode) {
				case RESULT_OK:
					Bundle bundle = data.getExtras();
					String startPlace = bundle.getString("start_place");
					String stopPlace = bundle.getString("stop_place");
					System.out.println(startPlace + "--" + stopPlace);
					hustMapView.searchPath(startPlace, stopPlace);
					break;

				default:
					break;
			}
			break;

		default:
			break;
		}
	}



	public static Bitmap getLoacalBitmap(String url) {
        try {
             FileInputStream fis = new FileInputStream(url);
             return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片         

          } catch (FileNotFoundException e) {
             e.printStackTrace();
             return null;
        }
   }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		
		menu.add(0, 1, 0, "搜索地点");
		menu.add(0, 2, 1, "查询路线");
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		
		LayoutInflater li = null;
		View view = null;
		AlertDialog.Builder builder = null;
		AlertDialog ad = null;
		
		switch(item.getItemId()){
			case 1:
				li = LayoutInflater.from(this);
				view = li.inflate(R.layout.choose_place_prompt_dialog, null);
				builder = new AlertDialog.Builder(this);
				builder.setTitle("搜索地点");
				builder.setView(view);
		
				ChoosePlacePromptListener cpPL = new ChoosePlacePromptListener(view);
				builder.setPositiveButton("确定", cpPL);
				builder.setNegativeButton("取消", cpPL);
				
				ad = builder.create();
				ad.show();
				break;
				
			case 2:
				li = LayoutInflater.from(this);
				view = li.inflate(R.layout.search_path_prompt_dialog, null);
				builder = new AlertDialog.Builder(this);
				builder.setTitle("查询路线");
				builder.setView(view);
		
				SearchPathPromptListener spPL = new SearchPathPromptListener(view);
				builder.setPositiveButton("确定", spPL);
				builder.setNegativeButton("取消", spPL);
				
				ad = builder.create();
				ad.show();
				break;
		}
		return true;
	}
    
	private class ChoosePlacePromptListener implements android.content.DialogInterface.OnClickListener
	{
		// local variable to return the prompt reply value
		private String promptReply = null;
		//Keep a variable for the view to retrieve the prompt value
		View promptDialogView = null;
		//Take in the view in the constructor
		public ChoosePlacePromptListener(View inDialogView) {
			promptDialogView = inDialogView;
		}
		
		//Call back method from dialogs
		public void onClick(DialogInterface v, int buttonId) {
			if (buttonId == DialogInterface.BUTTON1) {
				//ok button
				promptReply = getPromptText();
//				System.out.println(promptReply);
				hustMapView.choosePlace(promptReply);
			}
			else {
				//cancel button
//				promptValue = null;
			}
		}
		
		//Just an access method for what is in the edit box
		private String getPromptText() {
			EditText et = (EditText)promptDialogView.findViewById(R.id.editText_prompt);
			return et.getText().toString();
		}
		
		public String getPromptReply() { return promptReply; }
	}
	
	private class SearchPathPromptListener implements android.content.DialogInterface.OnClickListener
	{
		// local variable to return the prompt reply value
		private String promptStartPlace = null;
		private String promptStopPlace = null;
		//Keep a variable for the view to retrieve the prompt value
		View promptDialogView = null;
		//Take in the view in the constructor
		public SearchPathPromptListener(View inDialogView) {
			promptDialogView = inDialogView;
		}
		
		//Call back method from dialogs
		public void onClick(DialogInterface v, int buttonId) {
			if (buttonId == DialogInterface.BUTTON1) {
				//ok button
				promptStartPlace = getStartPromptText();
				promptStopPlace = getStopPromptText();
				hustMapView.searchPath(promptStartPlace, promptStopPlace);
			}
			else {
				//cancel button
//				promptValue = null;
			}
		}
		
		//Just an access method for what is in the edit box
		private String getStartPromptText() {
			EditText et = (EditText)promptDialogView.findViewById(R.id.start_place_prompt);
			return et.getText().toString();
		}
		
		private String getStopPromptText() {
			EditText et = (EditText)promptDialogView.findViewById(R.id.stop_place_prompt);
			return et.getText().toString();
		}

		public String getPromptStartPlace() {
			return promptStartPlace;
		}

		public String getPromptStopPlace() {
			return promptStopPlace;
		}
		
		
	}
    
}